package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.mapper.MenuRoleMapper;
import com.dk.pojo.MenuRole;
import com.dk.pojo.Role;
import com.dk.mapper.RoleMapper;
import com.dk.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public boolean insertRole(Role role, String[] mids) {
        //增加返回主键
        roleMapper.insertReturn(role);
        //增加角色菜单表
        int rmi = menuRoleMapper.insertBatchInfo(role.getRid(), mids);

        return rmi>0;
    }
    @Override
    public boolean updateRole(Role role, String[] mid) {
        int i = roleMapper.updateById(role);
        //删除角色菜单表数据
        QueryWrapper<MenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("rid",role.getRid());
        menuRoleMapper.delete(queryWrapper);
        int rmi = menuRoleMapper.insertBatchInfo(role.getRid(), mid);
        return rmi>0;
    }

    @Override
    public boolean deleteByRid(Long rid) {
        //删除角色表
        roleMapper.deleteById(rid);
        //删除角色中间表
        QueryWrapper<MenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("rid",rid);
        int delete = menuRoleMapper.delete(queryWrapper);
        return delete>0;
    }
}
